package com.wlient.basic.Stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.beans.beancontext.BeanContext;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class streamTest {

    public static void test1() {
        List<user> intList = insertData.insertUser();
        intList.stream().filter(m -> m.getAge() % 2 == 0).forEach(m -> System.out.println(m));

        System.out.println("----------------------------------------------------------------");
        intList.stream().filter(m -> m.getAge() % 2 != 0).forEach(System.out::println);


        System.out.println("----------------list-----------------------------------------");
        List list = intList.stream().map(m -> m.getName()).collect(Collectors.toList());
        System.out.println(list);


        System.out.println("----------------set-----------------------------------------");
        Set set = intList.stream().map(m -> m.getHeight()).collect(Collectors.toSet());
        System.out.println(set);

        System.out.println("--------------------分组---------------------------------------");
        Map<Integer, List<user>> collect = intList.stream().collect(Collectors.groupingBy(user::getHeight));
        collect.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });

        System.out.println("----------------------双重分组---------------------------------");
        Map<Integer, Map<Integer, List<user>>> collect2 = intList.stream().collect(Collectors.groupingBy(user::getHeight, Collectors.groupingBy(user::getAge)));
        collect2.forEach((k, v) -> {
            System.out.println(k + "---");
            v.forEach((K, V) -> System.out.println(K + ":" + V));
        });

        List<user> newUser = intList.stream().map(m -> {
            String name = m.getName();
            m.setName(name.replaceAll("(\\d{1})\\d{1}(\\d{1})", "$1****$2"));
            return m;
        }).collect(Collectors.toList());

        System.out.println(newUser);
    }

    public static void test2(){
        List<user> userList = insertData.insertUser();
        List<Address> addressList = insertData.insertAddress();
        List<userAddress> userAddressList = new ArrayList<>();
        Map<Integer,user> map = userList.stream().collect(Collectors.toMap(user::getId,user -> user));
        Iterator<Map.Entry<Integer,user>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer,user> entry = iterator.next();
            System.out.println("key:"+entry.getKey());
            System.out.println("value:"+entry.getValue());
        }
        addressList.stream().forEach(s->{
            if (map.containsKey(s.getId())){
                userAddress u = new userAddress();
                BeanUtils.copyProperties(map.get(s.getId()),u);
                u.setAddress(s.getAddress());
                userAddressList.add(u);
            }
        });
        System.out.println(userAddressList);
    }

    public static void main(String[] args) {
        test2();
    }

}
